/**
 * Copyright (c)  2010, William Ulrich
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <ORGANIZATION> nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <rika/control.h>

#include <sensor_msgs/Joy.h>
#include <math.h>

class ControlJS : public Control
{
public:
  ControlJS();

private:
  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
 
  // Joystick dev to listen to
  ros::Subscriber joy_sub;

  // Joystick params
  int linear_, angular_;
  double l_scale_, a_scale_;
 
};


ControlJS::ControlJS():
  Control(),
  linear_(1),
  angular_(2)
{
  nh_.param("axis_linear", linear_, linear_);
  nh_.param("axis_angular", angular_, angular_);
  nh_.param("scale_linear", l_scale_, l_scale_);
  nh_.param("scale_angular", a_scale_, a_scale_);

  joy_sub = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &ControlJS::joyCallback, this);
}

void ControlJS::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{        
  double vel = l_scale_ * joy->axes[linear_];
  double yaw = a_scale_ * joy->axes[angular_];

  //
  set_speed(vel, yaw);

}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "control_js");
  ControlJS rika_control_js;

  ros::spin();
}
